package com.atguigu.gulimall.seckill.scheduled;

import com.atguigu.gulimall.seckill.service.SeckillService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class SecSchedule {
    @Autowired
    private SeckillService seckillService;
    @Autowired
    private RedissonClient redissonClient;

    public static final String REDISSON_LOCK = "redisson:lock";
    @Scheduled(cron = "*/30 * * * * ?")
    @Async
    public void up3DaysSeckillProduct(){
        //利用分布式锁保证每次只有一个秒杀模块能顾进行上架
        RLock lock = redissonClient.getLock(REDISSON_LOCK);
        lock.lock(10, TimeUnit.SECONDS);
        try{
            seckillService.up3DaysSeckillProduct();
        }finally {
            lock.unlock();
        }
    }
}
